/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

package test;

import java.util.*;
import java.lang.*;
import java.io.IOException;
import bgtp.*;

public class ClientTest
{
    public static void main(String[] args)
    {
	TCPTransport		tcp    = null;
	OverHTTP		clt    = null;
	BGTPLog			logger = null;
	String			guid   = null;
	BGTPWorker		wrk    = null;
	BGTPmm			inst   = null;
	MQInterface		qu     = null;
	TreeMap<String, String> ms     = null;

	try
	    {
		tcp    = new TCPTransport("localhost", "10100");
		clt    = new OverHTTP(tcp.getClient());
		logger = new BGTPLog("TestClient");
		guid   = Registrator.doRegistration("NULL", clt, logger);
	    }
	catch(IOException ioe)
	    {
		ioe.printStackTrace();
		return;
	    }

	if(null == guid)
	    {
		logger.error("main", "null guid");
		return;
	    }

	inst = BGTPmm.getInstance();
	qu   = new MessageQueue();
	wrk  = new BGTPWorker(clt, inst, guid);

	inst.subscribe(new Filter("Action ~ '.*'"), qu);
	inst.start();
	inst.registerWorker(guid, wrk);

	for(int i = 0; i < 10; i++)
	    {
		ms = new TreeMap<String, String>();
		ms.put("ReqType", "GET / HTTP/1.1");
		ms.put("GUID", guid);
		ms.put("Action", "testAction_"+i);
		inst.send(ms);
		
		while(qu.size() == 0)
		    {
			Helper.sleep(100);
		    }

		if(qu.size() > 0)
		    {
			TreeMap<String, String> tmp = qu.get();
			for(String item : tmp.keySet())
			    {
				System.out.printf("%s: %s\n", item, tmp.get(item));
			    }
			System.out.println("");
		    }
	    }
	inst.shutdown();
	inst.join();
    }
}